package com.achievement.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.achievement.domain.dto.PaperLevelDTO;
import com.achievement.domain.entity.PaperLevelEntity;
import com.achievement.domain.param.PaperLevelParam;
import com.achievement.domain.vo.PaperLevelVO;
import com.achievement.mapper.PaperLevelMapper;
import com.achievement.mappings.PaperLevelMapping;
import com.achievement.service.IMagazineSourceService;
import com.achievement.service.IPaperLevelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author CNKI
* @description 针对表【dm_paper_level(科研成果-刊物级别列表)】的数据库操作Service实现
* @createDate 2025-02-28 14:53:32
*/
@Service
public class IPaperLevelServiceImpl extends ServiceImpl<PaperLevelMapper, PaperLevelEntity>
    implements IPaperLevelService {

    @Autowired
    private PaperLevelMapping mapping;

    @Autowired
    private IMagazineSourceService magazineSourceService;

    /**
     * 新增刊物级别
     *
     * @param dto dto
     */
    @Override
    public String add(PaperLevelDTO dto) {
        String UUID = IdUtil.randomUUID().replace("-", "");

        dto.setId(UUID);

        //新增刊物级别基本信息
        PaperLevelEntity entity = mapping.dto2Entity(dto);

        //期刊源信息
        if (StrUtil.isNotBlank(entity.getMagazineSourceIds())){
            List<String> list = Arrays.asList(entity.getMagazineSourceIds().split(","));
            String paperLevelID = entity.getId();
            magazineSourceService.bindPaperLevel(list,paperLevelID);
        }


        boolean save = this.save(entity);

        if (save){
            return entity.getId();
        }

        return "";
    }

    /**
     * 删除刊物级别
     *
     * @param ids ids
     */
    @Override
    public boolean batchRemove(List<String> ids) {

        //刊物级别，删除绑定期刊源
        if (CollUtil.isNotEmpty(ids)){
            magazineSourceService.deleteByPaperLevel(ids);
        }

        return this.removeByIds(ids);
    }

    /**
     * 修改刊物级别
     *
     * @param dto dto
     */
    @Override
    public boolean update(PaperLevelDTO dto) {
        PaperLevelEntity entity = mapping.dto2Entity(dto);

        String paperLevelID = entity.getId();

        List<String> ids = new ArrayList<>();
        ids.add(paperLevelID);

        //刊物级别，删除绑定期刊源
        if (CollUtil.isNotEmpty(ids)){
            magazineSourceService.deleteByPaperLevel(ids);
        }

        //再绑定期刊源信息
        if (StrUtil.isNotBlank(entity.getMagazineSourceIds())){
            List<String> list = Arrays.asList(entity.getMagazineSourceIds().split(","));
            magazineSourceService.bindPaperLevel(list,paperLevelID);
        }


        return this.updateById(entity);
    }

    /**
     * 列表检索
     *
     * @param param param
     */
    @Override
    public List<PaperLevelVO> selectByPage(PaperLevelParam param) {

        List<PaperLevelVO> paperLevelVOList = this.baseMapper.getList(param);

        return paperLevelVOList;
    }

    /**
     * 刊物级别导出
     *
     * @param dto dto
     */
    @Override
    public List<PaperLevelEntity> getListByConditions(PaperLevelDTO dto) {
        LambdaQueryWrapper<PaperLevelEntity> queryWrapper = new LambdaQueryWrapper();
        List<PaperLevelEntity> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 获取刊物级别详细信息
     *
     * @param id id
     */
    @Override
    public PaperLevelVO getInfoById(String id) {
        PaperLevelEntity entity = this.getById(id);
        if (ObjectUtil.isNull(entity)){
            return new PaperLevelVO();
        }

        PaperLevelVO paperLevelVO = mapping.entity2Vo(entity);

        return paperLevelVO;
    }
}




